﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;

public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindProject();
            Session.RemoveAll();
        }
    }

    private void BindProject()
    {
        ddlProjectList.DataSource = BLL_Factory.IBLL_Common.GetProjectList();
        ddlProjectList.DataTextField = "ProjectName";
        ddlProjectList.DataValueField = "ProjectId";
        ddlProjectList.DataBind();
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        CurrentAccount.ProjectId = int.Parse(ddlProjectList.SelectedValue);
        string code = txtCode.Text.Trim();
        if (IsValid)
        {
            if (CurrentAccount.VerifyValidateCode(code))
            {
                //if (string.Compare(txtCode.Text.Trim(), CurrentAccount.CheckCode, true) != 0 && !(System.Configuration.ConfigurationManager.AppSettings["DisableCheckCode"] != null && System.Configuration.ConfigurationManager.AppSettings["DisableCheckCode"] == "1"))
                //{
                //    txtCode.Text = "";
                //    HZ.Web.WebUtil.Alert("验证码输入错误", this);
                //    return;
                //}

                //配置文件里生产企业登录标识，免得像资信一样发布两次  04用CA登录
                string comca = System.Configuration.ConfigurationManager.AppSettings["comca"];
                if (comca == "04")
                {
                    //string roleid = Convert.ToString(HZ.Data.DbHelperFactory.Create().ExecuteScalar(string.Format(@"select AccountType from TB_Account where AccountName='{0}'", txtUserId.Text.Trim())));
                    //企业角色登录
             
                        HZ.Web.WebUtil.Alert("登录失败！用户请用CA登陆。", this);
                        return;
                }

                // 设置当前项目上下文
                Account account;
                var i = BLL_Factory.IBLL_Common.Login(txtUserId.Text.Trim(), txtUserPwd.Text.Trim(), out account);

                if (i == 2)
                {
                    HZ.Web.WebUtil.Alert("用户名不存在", this);
                    return;
                }

                if (i == 3)
                {
                    HZ.Web.WebUtil.Alert("用户名密码不匹配", this);
                    return;
                }

                if (i == 4)
                {
                    HZ.Web.WebUtil.Alert("系统连接错误", this);
                    return;
                }

                if (i == 5)
                {
                    HZ.Web.WebUtil.Alert("您没有品规需要报价！", this);
                    return;
                }


                if (i == 1)
                {
                    CurrentAccount.Account = account;
                    string ip = hfIP.Value;
                    HZ.Common.log4netManager.LogInfo("登录系统", string.Format("登录成功:用户编号{0},登录IP地址{1}", account.AccountName, ip), null);

                    if (account.AccountType.Value == 2)
                    {
                        HZ.BidSystem.BLL.BLL_Cache.SetOnlineModel(account.AccountId.Value, new OnLineModel() { Active = "刚刚登录" });
                        Response.Redirect("~/Company/Default.aspx");
                    }
                    else if (account.AccountType == 1 || account.AccountType == 0)
                    {
                        Response.Redirect("~/Manage/Default.aspx");
                    }
                    else if (account.AccountType == 3) //专家类型
                    {
                        Response.Redirect("~/Manage/ExpertDefault.aspx");
                    }
                    else
                    {
                        HZ.Web.WebUtil.Alert("禁止该类型账户登录系统！");
                        return;
                    }

                }
            }
            else
            {
                txtCode.Text = "";
                HZ.Web.WebUtil.Alert("验证码输入错误", this);
                return;
            }
        }
    }

    public string CheckCode { get; set; }
}
